package com.ztz.musicappdemo.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ztz.musicappdemo.MyDbHelper;
import com.ztz.musicappdemo.R;

public class LoginActivity extends BaseActivity{
    TextView tv_Register;
    EditText UserName,password;
    Button btn_Login;
    MyDbHelper dbHelper;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private CheckBox rememberPass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        pref= PreferenceManager.getDefaultSharedPreferences(this);
        dbHelper=new MyDbHelper(this,"UserInfoStore.db",null,1);
        tv_Register=(TextView)findViewById(R.id.tvRegister);
        UserName=(EditText)findViewById(R.id.editUser);
        password=(EditText)findViewById(R.id.editPwd);
        btn_Login=(Button)findViewById(R.id.btnLogin);
        rememberPass=(CheckBox)findViewById(R.id.rememberPassword);
        boolean isRemember=pref.getBoolean("remember_password",false);
        if(isRemember){
            String userName=pref.getString("userName","");
            String pwd=pref.getString("password","");
            UserName.setText(userName);
            password.setText(pwd);
            rememberPass.setChecked(true);
        }
        tv_Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
        btn_Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               String userName=UserName.getText().toString();
                String pwd=password.getText().toString();
                SQLiteDatabase db=dbHelper.getWritableDatabase();
                Cursor cursor=db.query("UserInfo",new String[]{"password"},"userName=?",new String[]{userName},null,null,null);
                if(cursor.moveToFirst()){
                    //存在这个用户
                    String pwdTemp=cursor.getString(0);
                    if(pwdTemp.equals(pwd)){
                        editor=pref.edit();
                        if(rememberPass.isChecked()){
                            editor.putBoolean("remember_pass",true);
                            editor.putString("userName",userName);
                            editor.putString("password",pwd);
                        }else {
                            editor.clear();
                        }
                        editor.apply();
                        Toast.makeText(LoginActivity.this, "密码输入正确", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                        startActivity(intent);
                    }else {
                        Toast.makeText(LoginActivity.this, "密码输入不正确,请重新输入", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(LoginActivity.this, "用户不存在,清注册", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}
